//
//  BaseTableView.m
//  ZzzzyManProject
//
//  Created by mac on 16/3/5.
//  Copyright © 2016年 ZzzzyMan@. All rights reserved.
//

#import "BaseTableView.h"

@interface BaseTableView()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSArray *groups;

@end

@implementation BaseTableView

- (NSArray *)groups {
    if (!_groups) {
        _groups = [self getGroups];
    }
    return _groups;
}


//子类重写,提供数据
- (NSArray *)getGroups {
    return nil;
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    self = [super initWithFrame:frame style:UITableViewStyleGrouped];
    self.delegate = self;
    self.dataSource = self;
    self.backgroundColor = [[UIColor alloc] initWithRed:231/255.0 green:231/255.0 blue:231/255.0 alpha:1];
    return self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.groups.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    InfoGroup * group = self.groups[section];
    return group.items.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    InfoCell * cell = [InfoCell cellWithTableView:tableView];
    InfoGroup * group = self.groups[indexPath.section];
    InfoItem * item = group.items[indexPath.row];
    cell.item = item;
    
    return cell;
}

#pragma mark - 选中Cell时执行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //通过判断模型来断定执行什么操作
    InfoGroup * group = self.groups[indexPath.section];
    InfoItem * item = group.items[indexPath.row];
    if ([item isKindOfClass:[InfoItemArrow class]]) {
        InfoItemArrow *arrow = (InfoItemArrow *)item;
        if (arrow.Controller) {
            
            //通知控制器弹出控制器
            [[NSNotificationCenter defaultCenter] postNotificationName:@"modalVC" object:arrow];
            
        }else if(arrow.option) {
            arrow.option();
        }
    }
    
}


@end
